package com.example.demo.service.Impl;

import com.example.demo.bean.Account;
import com.example.demo.dao.AccountMapper;
import com.example.demo.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public void save(Account account) {
     Account dbAccount=accountMapper.selectByAccount(account.getAccount());
   if(dbAccount!=null) {
       throw new RuntimeException("账号名已经被人占用");
   }
   account.setStatus("正常");
   account.setLoginTime(new Date().getTime());
        accountMapper.insert(account);
    }

    @Override
    public List<Account> findAll() {
        return accountMapper.selectAll();
    }
    @Override
    public Account findById(long id) {
        return accountMapper.selectByPrimaryKey(id);

    }
    @Override
    public void deleteById(long id) {
       Account account=this.findById(id);
     if(account==null ){
      throw new RuntimeException("该账号不存在");
         }
     accountMapper.deleteByPrimaryKey(id);
    }
}
